package com.jyh.controller;

//import com.alibaba.fastjson.JSON;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jyh.common.R;
import com.jyh.pojo.Employee;
import com.jyh.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RequestMapping("/employee")
@RestController
@Slf4j
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    //登录
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        //获取前端登录用户信息
        String username = employee.getUsername();
        String password = employee.getPassword();

        //将前端传入密码MD5加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<Employee>();
        lqw.eq(Employee::getUsername,employee.getUsername());

        Employee emp = employeeService.getOne(lqw);

        //查询结果为null，说明不存在该用户
        if (emp == null) return R.error("登录失败");

        //判断密码是否一致
        if (!emp.getPassword().equals(password)) return R.error("密码错误");

        //判断用户是否禁用
        if (emp.getStatus() == 0) return R.error("该用户已禁用，请联系管理员");

        //上述不满足，说明查到用户，密码正确，先获取session然后对其写入属性
        request.getSession().setAttribute("employee", emp.getId());

        return R.success(emp);
    }

    //退出
    @PostMapping("/logout")
    public R logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");

        return R.success("退出成功");
    }

    //添加员工
    @PostMapping
    public R addEmployee(HttpServletRequest httpServletRequest, @RequestBody Employee employee) {
        log.info("创建信息：" + employee);
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper();
        lqw.eq(Employee::getUsername,employee.getUsername());

        //避免重复注册
        Employee findEmployee = employeeService.getOne(lqw);
        if (findEmployee != null) return R.error("该用户已存在");

        //初始密码为123456，使用md5加密后存储
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        employeeService.save(employee);
        return R.success("success");
    }

    //分页查询
    @GetMapping("/page")
    public R<Page> selectPage(int page, int pageSize, String name) {
//        log.info("page:{},pageSize:{},name:{}", page, pageSize, name);
        //分页器
        Page pageInfo = new Page<>(page,pageSize);
        pageInfo.setOptimizeCountSql(false);

        //包装器，设置条件
        LambdaQueryWrapper<Employee> lwq=new LambdaQueryWrapper();

        //过滤条件,成立才包装
        //执行逻辑，默认name为空，条件成立，封装wrapper，然后查询语句就是查找null不能返回正常数据
        lwq.like(!StringUtils.isEmpty(name),Employee::getName,name);
        employeeService.page(pageInfo,lwq);

        return R.success(pageInfo);
    }

    //更改员工
    @PutMapping
    public R updateEmployee(HttpServletRequest request, @RequestBody Employee employee) {
//        log.info("当前对象：{}",employee);

//        log.info("controller.updateEmployee线程：{}",Thread.currentThread().getId());

        employeeService.updateById(employee);

        return  R.success("success") ;
    }

    //查询员工
    @GetMapping("/{id}")
    public R<Employee> getEmployeeById(@PathVariable Long id) {
        Employee employee = employeeService.getById(id);
        if (employee != null) return R.success(employee);
        return R.error("查询失败");
    }
}
